﻿// Inner Fire 游戏引擎库
// DxShadows 渲染器光源功能
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 用于DirectX的渲染器。
// 此文件分装用于光源功能
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-04-23

#pragma once

#include "dx_module.h"
#include <dx/core/frame_resource.h>
#include <dx/texture/shadow_map.h>
#include <memory>
#include <DirectXCollision.h>

namespace ifire::dx {
class DxGraphics;

class DxShadows : public DxModule {
public:
  DxShadows(DxGraphics* dx);
  DxShadows(const DxShadows& rhs) = delete;
  DxShadows& operator=(const DxShadows& rhs) = delete;
  ~DxShadows() = default;

  void InitConfig();
  void Init();
  void UpdateShadowTransform();
  void UpdateShadowPass();

  // ------
  // 属性访问
  // ------
  DirectX::XMFLOAT4X4& LightView() { return light_view_; }
  DirectX::XMFLOAT4X4& LightProjection() { return light_proj_; }
  DirectX::XMFLOAT3& LightPos() { return light_pos_w_; }
  float GetNearZ() const { return light_near_z_; }
  float GetFarZ() const { return light_far_z_; }
  void SetNearZ(float value) { light_near_z_ = value; }
  void SetFarZ(float value) { light_far_z_ = value; }
  ShadowMap* GetShadowMap() { return shadow_map_.get(); }
  DirectX::XMFLOAT4X4& GetShadowTransform() { return shadow_transform_; }

private:
  // 用于阴影
  std::unique_ptr<ShadowMap> shadow_map_;
  PassConstants shadow_pass_; // index 1 of pass cbuffer.
  DirectX::XMFLOAT4X4 shadow_transform_ = IDENTITY_4X4;
  DirectX::BoundingSphere scene_bounds_;
  float light_near_z_ = 0.0f;
  float light_far_z_ = 0.0f;
  DirectX::XMFLOAT3 light_pos_w_;
  DirectX::XMFLOAT4X4 light_view_ = IDENTITY_4X4;
  DirectX::XMFLOAT4X4 light_proj_ = IDENTITY_4X4;
};

} // namespace ifire::dx